﻿using MicroCloud.Pack.Identity.Entities;
using SettingInputDto = Vinno.Brand.Pack.Core.Dtos.SettingInputDto;
using SettingOutputDto = Vinno.Brand.Pack.Core.Dtos.SettingOutputDto;


namespace Vinno.Brand.Pack.Data
{
    /// <summary>
    /// 权限模块DTO实体映射类
    /// </summary>
    public class BrandMapperConfiguration : AutoMapperTupleBase
    {
        /// <summary>
        /// 获取 服务提供者
        /// </summary>
        protected IServiceProvider ServiceProvider { get; }

        public BrandMapperConfiguration(IServiceProvider provider)
        {
            ServiceProvider = provider;
        }

        /// <summary>
        /// 创建对象映射
        /// </summary>
        public override void CreateMap()
        {
            CreateMap<SettingInputDto, SettingDto>()
              .ForMember(t => t.VinnoVideoImgId, opt => opt.MapFrom(s => s.VinnoVideoImgs.FirstOrDefault().Id))
              .ForMember(t => t.VinnoInviteImageId, opt => opt.MapFrom(s => s.VinnoInviteImgs.FirstOrDefault().Id));

            CreateMap<SettingDto, SettingOutputDto>()
                .ForMember(t => t.VinnoVideoImgs, opt => opt.MapFrom(s => GetFileEntity(ServiceProvider, s.VinnoVideoImgId)))
                .ForMember(t => t.VinnoInviteImgs, opt => opt.MapFrom(s => GetFileEntity(ServiceProvider, s.VinnoInviteImageId)));

            CreateMap<SpaceInfoInputDto, SpaceInfo>()
                .ForMember(t => t.ThumbImgId, opt => opt.MapFrom(s => s.ThumbImgs.FirstOrDefault().Id));
            CreateMap<SpaceInfo, SpaceInfoOutputDto>()
                .ForMember(t => t.ThumbImgs, opt => opt.MapFrom(s => new FileEntity[] { s.ThumbImg }));

            CreateMap<ProductCarouselInputDto, ProductCarousel>()
                .ForMember(t => t.ImgId, opt => opt.MapFrom(s => s.Imgs.FirstOrDefault().Id));
            CreateMap<ProductCarousel, ProductCarouselOutputDto>()
                .ForMember(t => t.Imgs, opt => opt.MapFrom(s => new FileEntity[] { s.Img }));

            CreateMap<ProductInputDto, Product>()
                .ForMember(t => t.TitleImgId, opt => opt.MapFrom(s => s.TitleImgs.FirstOrDefault().Id))
                .ForMember(t => t.VideoImgId, opt => opt.MapFrom(s => s.VideoImgs.FirstOrDefault().Id));
            CreateMap<Product, ProductOutputDto>()
                .ForMember(t => t.TitleImgs, opt => opt.MapFrom(s => new FileEntity[] { s.TitleImg }))
                .ForMember(t => t.VideoImgs, opt => opt.MapFrom(s => new FileEntity[] { s.VideoImg }));

            CreateMap<ProductCaseInputDto, ProductCase>()
                .ForMember(t => t.CaseImgId, opt => opt.MapFrom(s => s.CaseImgs.FirstOrDefault().Id));
            CreateMap<ProductCase, ProductCaseOutputDto>()
                .ForMember(t => t.CaseImgs, opt => opt.MapFrom(s => new FileEntity[] { s.CaseImg }));

        }

        protected static FileEntity[] GetFileEntity(IServiceProvider provider, long fileId)
        {
            var files = Array.Empty<FileEntity>();
            using IServiceScope scope = provider.CreateScope();
            var fileRepository = scope.ServiceProvider.GetService<IRepository<FileEntity, long>>();
            var file = fileRepository.Get(fileId);
            if (file != null)
            {
                files = new FileEntity[] { file };
            }
            return files;
        }

    }
}
